package spring_annotation.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.UUID;

@Repository
public class StudentDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void insert() {
        String sql = "insert into t_student (first_name,last_name,gender,user,password,class_id) values (?, ?, ?, ?, ?, ?)";
        String u_string = UUID.randomUUID().toString();
        String first_name = u_string.substring(0, 5);
        String last_name = u_string.substring(u_string.length() - 5);
        Integer gender = (int) (1 + Math.random() * 2);
        Integer class_id = (int) (1 + Math.random() * 1000);
        String user = u_string.substring(5, 10);
        String pwd = u_string.substring(10, 15);
        jdbcTemplate.update(sql, first_name, last_name, gender, user, pwd, class_id);
    }

    public void insertClass() {
        String sql = "insert into t_class (number,type,level,begin) values(?, ?, ?, ?);";
        String[] types = new String[]{"语文","数学","英语","物理","化学","生物"};
        String[] levels = new String[]{"小学","初中","高中","大学"};
        Integer number = (int) (30 + Math.random() * 30);
        String type = types[(int) (Math.random() * 6)];
        String level = levels[(int) (Math.random() * 4)];
        Integer begin = (int) (1 + Math.random() * 2);
        jdbcTemplate.update(sql, number, type, level, begin);
    }

    public void updateStudent(int i) {
        String sql = "update t_student set password = ? where id = ?;";
        String pwd = UUID.randomUUID().toString();
        jdbcTemplate.update(sql, pwd, i);
    }
}
